package org.easy.elasticsearch.config.pool;

import org.apache.commons.pool2.KeyedPooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.http.HttpHost;
import org.easy.elasticsearch.config.client.ElasticSearchClient;
import org.easy.elasticsearch.config.pool.EsClientPoolConfigure;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @Title: ElasticSearchPoolFactory.java
 * @Package org.easy.elasticsearch.config.pool
 * @Description: TODO
 * @author linNaibin
 * @date 2020年9月14日 上午10:44:12
 * @version V1.0
 */
@Component
public class ElasticSearchPoolFactory implements KeyedPooledObjectFactory<String, ElasticSearchClient> {
	private final Logger logger = LoggerFactory.getLogger(ElasticSearchPoolFactory.class);

	@Autowired
	private EsClientPoolConfigure elasticSearchPoolConfig;

	/**
	 * 获取连接
	 *
	 * @param s 集群名称
	 * @return
	 * @throws Exception
	 */
	@Override
	public PooledObject<ElasticSearchClient> makeObject(String s) throws Exception {
		List<HttpHost> httpHosts = new ArrayList<>();
		for (EsClientPoolConfigure.ElasticSearchClusterConfig elasticSearchClusterConfig : elasticSearchPoolConfig
				.getPool()) {
			if (s.equals(elasticSearchClusterConfig.getClusterName())) {
				for (EsClientPoolConfigure.ElasticSearchClusterConfig.ElasticSearchNodeConfig elasticSearchNodeConfig : elasticSearchClusterConfig
						.getNodes()) {
					httpHosts.add(new HttpHost(elasticSearchNodeConfig.getIp(), elasticSearchNodeConfig.getPort(),
							elasticSearchNodeConfig.getSchema()));
				}
			}
		}
		
		System.out.println("httpHosts size >>> :::: "+httpHosts.size());

		HttpHost[] httpHosts1 = new HttpHost[httpHosts.size()];
		httpHosts1 = httpHosts.toArray(httpHosts1);

		// builder
		System.out.println("httpHosts1 :::: "+httpHosts1);
		RestClientBuilder restClientBuilder = RestClient.builder(httpHosts1);
		
		// set 
 
		RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
		ElasticSearchClient elasticSearchClient = new ElasticSearchClient(restHighLevelClient, s);
		return new DefaultPooledObject<ElasticSearchClient>(elasticSearchClient);
	}

	/**
	 * 销毁客户端
	 *
	 * @param s
	 * @param pooledObject
	 * @throws Exception
	 */
	@Override
	public void destroyObject(String s, PooledObject<ElasticSearchClient> pooledObject) throws Exception {
		ElasticSearchClient elasticSearchClient = pooledObject.getObject();
		if (elasticSearchClient == null) {
			pooledObject = null;
			return;
		}
		if (elasticSearchClient.getRestHighLevelClient() == null) {
			pooledObject = null;
			return;
		}
		RestHighLevelClient restHighLevelClient = elasticSearchClient.getRestHighLevelClient();
		if (restHighLevelClient.ping(RequestOptions.DEFAULT)) {
			restHighLevelClient.close();
			pooledObject = null;
		}
	}

	/**
	 * 验证客户端有效性
	 *
	 * @param s
	 * @param pooledObject
	 * @return
	 */
	@Override
	public boolean validateObject(String s, PooledObject<ElasticSearchClient> pooledObject) {
		ElasticSearchClient elasticSearchClient = pooledObject.getObject();
		if (elasticSearchClient == null) {
			return false;
		}
		if (elasticSearchClient.getRestHighLevelClient() == null) {
			return false;
		}
		RestHighLevelClient restHighLevelClient = elasticSearchClient.getRestHighLevelClient();
		try {
			return restHighLevelClient.ping(RequestOptions.DEFAULT);
		} catch (Exception e) {
			logger.error("集群" + s + "验证es客户端有效性失败！", e);
		}
		return false;
	}

	/**
	 * 唤醒客户端
	 *
	 * @param s
	 * @param pooledObject
	 * @throws Exception
	 */
	@Override
	public void activateObject(String s, PooledObject<ElasticSearchClient> pooledObject) throws Exception {
//        RestHighLevelClient restHighLevelClient = pooledObject.getObject();
	}

	/**
	 * 挂起客户端
	 *
	 * @param s
	 * @param pooledObject
	 * @throws Exception
	 */
	@Override
	public void passivateObject(String s, PooledObject<ElasticSearchClient> pooledObject) throws Exception {
//        RestHighLevelClient restHighLevelClient = pooledObject.getObject();
	}
}
